
import Chalyx
from Chalyx.base import SigProxyObj, ProxiedSignal
from sepal import Sepal

class Player(Sepal, SigProxyObj):

	def __init__(self, service, library):
		Sepal.__init__(self, service)
		SigProxyObj.__init__(self)
		self.library = library
		self.signals = {
			"playtime": ProxiedSignal("playtime", self.service, "pb_playtime", self._faketrans, "playtime"),
			"status": ProxiedSignal("status", self.service, "pb_status", self.on_status_change, "status"),
			"stream_info": ProxiedSignal("stream_info", self.service, "pb_current_id", self._stream_info, None)
		}
		self.status = Chalyx.STATUS_UNKNOWN
		self.curInfo = {"id": -1}

	def play(self, *args):
		self.service.playback_start()

	def pause(self, *args):
		self.service.playback_pause()

	def play_pause_toggle(self, *args):
		self.service.playback_play_pause_toggle()

	def stop(self, *args):
		self.service.playback_stop()
	
	def seek_ms(self, ms, *args):
		self.service.playback_seek_ms(ms)
	
	def get_status(self, cb):
		def __status_cb(status):
			self.status = status
			cb(self, status)
		self.service.playback_status(__status_cb)
	
	def cur_stream_info(self, cb, udata=None):
		def __info_cb(info):
			self.curInfo = info
			cb(self, info, udata)
		self.service.info_current_stream(__info_cb)
	
	def _stream_info(self, obj, sigdata, udata):
		def __info_cb(data, self):
			self.curInfo = data
			self.emit("stream_info", data)
		self.library.item_info(sigdata, __info_cb, self)

	def on_status_change(self, obj, status, udata):
		assert status in [Chalyx.STATUS_UNKNOWN,
						  Chalyx.STATUS_STOPPED,
						  Chalyx.STATUS_PLAYING,
						  Chalyx.STATUS_PAUSED]
		self.status = status
		self.emit("status", self.status)
